package com.wbc.smartHome.service;

import com.wbc.smartHome.dao.CommodityDao;
import com.wbc.smartHome.dao.CommunityActivitiesDao;
import com.wbc.smartHome.dao.LiveTVDao;
import com.wbc.smartHome.model.Commodity;
import com.wbc.smartHome.model.CommunityActivities;
import com.wbc.smartHome.model.LiveTV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class RecommendService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CommodityDao commodityDao;
    @Autowired
    private LiveTVDao liveTVDao;
    @Autowired
    private CommunityActivitiesDao communityActivitiesDao;


    public ArrayList<Object> getRecommendList() {
       /* ListOperations listOps  = redisTemplate.opsForList();
        List<Commodity> commodityList = listOps.range("commodity", 0, -1);
        List<LiveTV> liveTVList = listOps.range("livetv", 0, -1);
        List<CommunityActivities> communityActivitiesList = listOps.range("communityactivities", 0, -1);*/

        List<Commodity> commodityList = commodityDao.getRecommendCommodityList();
        List<LiveTV> liveTVList = liveTVDao.getLiveTVList();
        List<CommunityActivities> communityActivitiesList = communityActivitiesDao.getCommunityActivitiesList();

        ArrayList<Object> arrayList = new ArrayList<>();

        // 将 commodityList 中的数据添加到 recommendList 中
        if (commodityList != null) {
            arrayList.addAll(commodityList);
        }

        // 将 liveTVList 中的数据添加到 recommendList 中
        if (liveTVList != null) {
            arrayList.addAll(liveTVList);
        }

        // 将 communityActivitiesList 中的数据添加到 recommendList 中
        if (communityActivitiesList != null) {
            arrayList.addAll(communityActivitiesList);
        }

        // 生成五个不重复的随机索引
        Set<Integer> indexSet = new HashSet<>();
        Random random = new Random();

        while (indexSet.size() < 5 && indexSet.size() < arrayList.size()) {
            indexSet.add(random.nextInt(arrayList.size()));
        }

        // 将随机索引中对应的元素存入新的ArrayList
        ArrayList<Object> recommendList = new ArrayList<>();
        for (Integer index : indexSet) {
            recommendList.add(arrayList.get(index));
        }

        return recommendList;
    }

}
